﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DomainModel.Concrete;
using DomainModel.Entities;
using System.IO;
using System.Drawing;

namespace WebUI.Controllers
{
    public class PictureController : Controller
    {
        SqlPictureRepository _pictureRepository;

        public PictureController()
        {
            _pictureRepository = new SqlPictureRepository();
        }

        public int SavePictureFile(Picture picture, HttpPostedFileBase pictureOrignalFile, int maxWidth, int maxHeight)
        {
            try
            {
                System.Drawing.Bitmap theImage = ResizeImage(pictureOrignalFile, maxWidth, maxHeight);
                string domainPath = AppDomain.CurrentDomain.BaseDirectory + "/Content/Pictures" + "/";
                picture.PictureContentSize = pictureOrignalFile.ContentLength;
                picture.PictureMimeType = pictureOrignalFile.ContentType;
                picture.LastUpdate = System.DateTime.Now;

                if (picture.PictureID == 0)
                {
                    picture.PictureFileName = picture.PictureFileName + Path.GetExtension(pictureOrignalFile.FileName);
                    picture.PictureAltText = picture.PictureCaption;
                    _pictureRepository.Insert(picture);
                }
                else
                {
                    _pictureRepository.Update(picture);
                }

                string filePath = domainPath + picture.PictureFileName;
                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }
                theImage.Save(filePath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return picture.PictureID;
        }

        private System.Drawing.Bitmap ResizeImage(HttpPostedFileBase pictureFile, int maxWidth, int maxHeight)
        {
            try
            {
                string type = pictureFile.ContentType;
                byte[] snap = new byte[pictureFile.ContentLength];
                System.IO.Stream stream = pictureFile.InputStream;

                System.Drawing.Image originalImage = System.Drawing.Bitmap.FromStream(stream);


                // get the ratio of image dimensions to proposed new iomage bounds
                float originalImageWidthToMaxWidthRatio = (float)maxWidth / (float)originalImage.Width;
                float originalImageHeightToMaxHeightRatio = (float)maxHeight / (float)originalImage.Height;

                // get the ratio of image side that is smallest so we know when we resize keeping aspect ratio that the image will stay inside the bounds
                float resizeCoeffient = (originalImageWidthToMaxWidthRatio < originalImageHeightToMaxHeightRatio) ? originalImageWidthToMaxWidthRatio : originalImageHeightToMaxHeightRatio;

                // create a blank image to paint the new picture onto and set its resolution
                Bitmap newImageCanvas = new Bitmap(maxWidth, maxHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                newImageCanvas.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);

                // find the location to draw the resized image onto the new canvas
                int newImageX = (originalImageWidthToMaxWidthRatio < originalImageHeightToMaxHeightRatio) ? 0 : ((int)((maxWidth - (originalImage.Width * resizeCoeffient)) / 2));
                int newImageY = newImageX != 0 ? 0 : ((int)((maxHeight - (originalImage.Height * resizeCoeffient)) / 2));

                // create the new image
                using (Graphics g = Graphics.FromImage(newImageCanvas))
                {
                    // set the image background to white
                    g.Clear(System.Drawing.Color.Black);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                    // draw the rescaled original image onto the new canvas
                    g.DrawImage(originalImage,
                        new Rectangle(newImageX, newImageY, (int)(originalImage.Width * resizeCoeffient), (int)(originalImage.Height * resizeCoeffient)),
                        new Rectangle(0, 0, (int)originalImage.Width, (int)originalImage.Height),
                        GraphicsUnit.Pixel);
                }

                // return the reulsting scaled image on its background canvas
                return newImageCanvas;

            }
            catch (Exception ex)
            {
                throw ex;

            }
        }
    }
}
